package com.ruoyi.im;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.UnAuth;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.im.dao.IMArea;
import com.ruoyi.im.entity.IMSysArea;
import com.ruoyi.im.service.IMAreaService;
import com.ruoyi.im.vo.IMAreaTreeVo;
import com.ruoyi.setting.domain.LsStationLetter;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 魔金商城 on 18/6/14.
 * 消息控制器
 */
@Api(description = "消息接口")
@RestController
@Slf4j
public class IMAreaController {

    /**
     * 注入消息服务接口
     */
    @Autowired
    private IMAreaService areaService;
    @Autowired
    private RedisCache redisService;

    /**
     * 查询用户消息
     *
     * @return 返回区域列表树
     */
    @UnAuth
    @ResponseBody
    @RequestMapping(value = "/area/listHead", method = RequestMethod.GET)
    @ApiOperation(value = "查询区域消息", notes = "查询区域消息", httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回区域消息", response = IMAreaTreeVo.class)
    })
    public AjaxResult queryAreaHead() {
        return AjaxResult.success();
    }

    /**
     * 查询区域消息
     *
     * @param area 区域编码
     * @return 返回区域列表树
     */
    @UnAuth
    @RequestMapping(value = "/area/listChild", method = RequestMethod.POST)
    @ApiOperation(value = "查询区域消息", notes = "查询区域消息", httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回区域消息", response = IMAreaTreeVo.class)
    })
    public AjaxResult queryAreaChild(@RequestBody @Validated @ApiParam(value = "查询区域接口", required = true) IMArea area) {
        String areaTree = redisService.getValue(area.getAreaCode());
        if(areaTree != null && !areaTree.isEmpty()) {
            return AjaxResult.success(areaTree);
        }
        IMSysArea sysArea =new IMSysArea();
        sysArea.setAreaCode(area.getAreaCode());
        if (StringUtils.isNotEmpty(area.getLat())) {
            sysArea.setLat(Float.parseFloat(area.getLat()));
            sysArea.setLng(Float.parseFloat(area.getLng()));
        }
        List<IMAreaTreeVo> listAreaTree =  areaService.listAreaTree(sysArea);
        if (listAreaTree != null && !listAreaTree.isEmpty()) {
            redisService.putToRedis(area.getAreaCode(),JSON.toJSONString(listAreaTree));
        }
        return AjaxResult.success(listAreaTree);
    }

    /**
     * 查询区县列表
     *
     * @param cityCode 城市编码
     * @return 返回区域列表树
     */
    @UnAuth
    @RequestMapping(value = "/area/querycountybycityid", method = RequestMethod.GET)
    @ApiOperation(value = "查询区县消息", notes = "查询区域消息", httpMethod = "GET")
    public AjaxResult queryAreaById(String cityCode) {
        if(cityCode == "" || cityCode == null) {
            AjaxResult.error("城市编码不能为空");
        }
        IMSysArea sysArea =new IMSysArea();
        sysArea.setAreaCode(cityCode);
        List<IMAreaTreeVo> listAreaTree =  areaService.listAreaByCityId(sysArea);
        return AjaxResult.success(listAreaTree);
    }

    /**
     * 查询乡镇列表
     *
     * @param countyCode 县编码
     * @return 返回区域列表树
     */
    @UnAuth
    @RequestMapping(value = "/area/querytownbycountyid", method = RequestMethod.GET)
    @ApiOperation(value = "查询乡镇列表", notes = "查询区域消息", httpMethod = "GET")
    public AjaxResult queryAreaById1(String countyCode) {
        if(countyCode == "" || countyCode == null) {
            AjaxResult.error("区县编码不能为空");
        }
        IMSysArea sysArea =new IMSysArea();
        sysArea.setAreaCode(countyCode);
        List<IMAreaTreeVo> listAreaTree =  areaService.listAreaByCountyId(sysArea);
        return AjaxResult.success(listAreaTree);
    }

    /**
     * 查询村庄列表
     *
     * @param townCode 乡镇编码
     * @return 返回区域列表树
     */
    @UnAuth
    @RequestMapping(value = "/area/queryvillagebytownid", method = RequestMethod.GET)
    @ApiOperation(value = "查询村庄消息", notes = "查询村庄消息", httpMethod = "GET")
    public AjaxResult queryAreaById2(String townCode) {
        if(townCode == "" || townCode == null) {
            AjaxResult.error("乡镇编码不能为空");
        }
        IMSysArea sysArea =new IMSysArea();
        sysArea.setAreaCode(townCode);
        List<IMAreaTreeVo> listAreaTree =  areaService.listAreaByTownId(sysArea);
        return AjaxResult.success(listAreaTree);
    }
}
